﻿using Furion.DependencyInjection;
using Microsoft.Extensions.Caching.Memory;
using MyProject.Entity;
using MyProject.Enum;
using MyProject.IService.SystemManage;
using MyProject.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyProject.Cache
{
    public class MenuAuthorizeCache : ITransient
    {
        private readonly string MenuAuthorizeCacheKey = CacheKeys.MenuAuthorizeCache.ParseToString();

        private readonly IMemoryCache _memoryCache;
        private readonly IMenuAuthorizeService _menuAuthorizeService;

        public MenuAuthorizeCache(IMemoryCache memoryCache, IMenuAuthorizeService menuAuthorizeService)
        {
            _memoryCache = memoryCache;
            _menuAuthorizeService = menuAuthorizeService;
        }

        /// <summary>
        /// 查询缓存中的权限
        /// </summary>
        public async Task<List<MenuAuthorizeEntity>> GetMenyAuthorizeList()
        {
            var cacheList = _memoryCache.Get<List<MenuAuthorizeEntity>>(MenuAuthorizeCacheKey);
            if (cacheList == null|| cacheList.Count() == 0)
            {
                cacheList = await _menuAuthorizeService.GetList(null);
                _memoryCache.Set(MenuAuthorizeCacheKey, cacheList);
            }
            return cacheList;
        }

        public void Remove()
        {
            _memoryCache.Remove(MenuAuthorizeCacheKey);
        }
    }
}
